package com.wxy.ej.flow.modules.act.listen;

import com.wxy.ej.flow.modules.act.bean.FormData;
import com.wxy.ej.flow.modules.act.dto.FormDataDto;
import com.wxy.ej.flow.modules.act.service.beanService.FormDataService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 修改业务主任务状态
 */
@Slf4j
@Component("mainTaskCompleteListener")
@Scope("prototype")
public class MainTaskCompleteListener implements ExecutionListener {
    @Resource
    FormDataService formDataService;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        log.info("---->act listener: MainTaskCompleteListener");
        System.out.println("=======主流程完成 MainTaskCompleteListener ====== NodeName: "+delegateExecution.getCurrentFlowElement().getName());
        System.out.println("EventName:" + delegateExecution.getEventName());
        System.out.println("ProcessDefinitionId:" + delegateExecution.getProcessDefinitionId());
        System.out.println("ProcessInstanceId:" + delegateExecution.getProcessInstanceId());

        // user: {answerId=295, userName=test-13222333322, userId=33}
        // 是这个用户完成任务
        List<Map> companyList = (List<Map>)delegateExecution.getVariable("companyList");
        List<Map> userList = (List<Map>)delegateExecution.getVariable("userList");
        Map user = (Map)delegateExecution.getVariable("user");

        System.out.println("user: "+user);
        System.out.println("userList: "+userList);
        System.out.println("companyList: "+companyList);

        FormData formData = new FormData();
        //formDataDto.setNodeName(delegateExecution.getEventName()); // the value is: end
        formData.setProcDefId(delegateExecution.getProcessDefinitionId());
        formData.setProcInstId(delegateExecution.getProcessInstanceId());
        formDataService.createBusinessEndNode(formData);
/*
        // 流程执行日志
        FlowElement flowElement = delegateExecution.getCurrentFlowElement();
        // ${companyId}
        String doc = ExpressionUtils.parse(flowElement.getDocumentation(), delegateExecution);


        CtcTaskLogBizDto taskLogDto = new CtcTaskLogBizDto();
        taskLogDto.setCtcTaskId(Long.parseLong(delegateExecution.getProcessInstanceBusinessKey()));
        taskLogDto.setFlowElementId(flowElement.getId());
        taskLogDto.setFlowElementName(flowElement.getName());
        taskLogDto.setFlowElementDoc(doc);
        taskLogDto.setCreatedate(DateTool.currentDate("yyyy-MM-dd HH:mm:ss.SSS"));
        StringBuilder sb = new StringBuilder();
        sb.append("主流程结束；修改业务表状态：主任务结案");
        taskLogDto.setDescription(sb.toString());
        ctcTaskLogBizService.create(taskLogDto);

        // 完成主任务
        cityTaskAutoService.completeBizTask(delegateExecution.getProcessInstanceBusinessKey(), false, true);


        // 删除变量
        delegateExecution.removeVariable("assigneeCount");
        delegateExecution.removeVariable("MainTaskEventRuleNum");
        delegateExecution.removeVariable("TASK_NAME");
        //
        delegateExecution.removeVariable("userList");
        delegateExecution.removeVariable("userNoticeList");
        delegateExecution.removeVariable("companyList");
        delegateExecution.removeVariable("companyIdList");
        delegateExecution.removeVariable("companyNameList");
        // condition
        delegateExecution.removeVariable("cmpyConditionRadio");
        delegateExecution.removeVariable("cmpyConditionCount");
        delegateExecution.removeVariable("cmpyConditionRate");
        delegateExecution.removeVariable("userConditionRadio");
        delegateExecution.removeVariable("userConditionCount");
        delegateExecution.removeVariable("userConditionRate");*/

    }
}
